class Admin::ProductsController < Admin::BaseController
  include PaginatorHelper
  
  def index
    parameters = create_parameters(params)
    @products = Admin.find_products_for_admin_by_parameters(parameters)
    
    
    
    if(params[:page] != nil) then
      @page = params[:page].to_i
    else
      @page = 0 
    end
    hash_product_list = get_shortened_list(@products,@page)
    @products = hash_product_list[:object]
    @indices = hash_product_list[:indices]
  end


  def new
    @product = Product.new
    @categories = Category.all
    @attributes = Attribute.all
  end


  def edit
    @product = Product.find(params[:id])

    @categories = Category.all
    @attributes = Attribute.all
  end


  def create
    @product = Product.new(params[:product])

    if @product.save
      flash[:notice] = 'Product is aangemaakt.'
      redirect_to edit_admin_product_path(@product)
    else
      render :action => "new"
    end
  end


  def update
    @product = Product.find(params[:id])

    if @product.update_attributes(params[:product])
      flash[:notice] = 'Product is aangepast.'
      redirect_to product_path(@product)
    else
      render :action => "edit"
    end
  end

  def destroy
    if(params[:product_id] != nil)
      params[:id] = params[:product_id]
    end
    @product = Product.find(params[:id])
    @product.destroy

    flash[:notice] = 'Het product is verwijderd.'
    create_parameters(params)
    redirect_to admin_products_path(@params)
  end
  
  private
  #creeert de parameters die worden meegegeven om de textvelden in de index ingevuld te houden.
  def create_parameters(params)
    parameters = {:search=>{:and=>{:CATEGORY => "", "products.name" => {:like => ""},"products.price" => {:gt => "",:st=>""}}}}
    @params = {}
    if(params == nil)
      params = {}
    end
    if(params[:title] != nil)
      parameters[:search][:and]['products.name'][:like] = params[:title]
      @params[:title] = params[:title]
      @title = params[:title]
    end
    if(params[:category] != nil)
      parameters[:search][:and][:CATEGORY] = params[:category]
      @params[:category] = params[:category]
      @category = params[:category]
    end
    if(params[:price_from] != nil)
      parameters[:search][:and]['products.price'][:gt] = params[:price_from]
      @params[:price_from] = params[:price_from]
      @price_from = params[:price_from]
    end
    if(params[:price_to] != nil)
      parameters[:search][:and]['products.price'][:st] = params[:price_to]
      @params[:price_to] = params[:price_to]
      @price_to = params[:price_to]
    end
    if(params[:sort] != nil)
      parameters[:sort] = params[:sort]
      @params[:sort] = params[:sort]
    end
    if(params[:sort_on] != nil)
      parameters[:sort_on] = params[:sort_on]
      @params[:sort_on] = params[:sort_on]
    end
    if(params[:page] != nil)
      @params[:page] = params[:page]
    end
    return parameters
  end  
end